package org.xinjiang.data.migration.flush.convert;

import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.xinjiang.data.migration.flush.enums.AccountStatusEnum;
import org.xinjiang.data.migration.flush.enums.AccountTypeEnum;
import org.xinjiang.data.migration.flush.enums.SexEnum;
import org.xinjiang.data.migration.flush.utils.PasswordUtil;
import org.xinjiang.data.migration.hainan.entity.SysUser;

import java.util.ArrayList;
import java.util.Collection;

;

/**
 * @author: 邢卫
 * @date: 2023年12月15日 10:35
 */
@Component
public class UserConvert extends BaseConvert<org.xinjiang.data.migration.xinjiang.entity.WegovOrgUser, SysUser> {


    private final String DEFAULT_PASSWORD = "abcd123.";


    @Override
    public SysUser convert(org.xinjiang.data.migration.xinjiang.entity.WegovOrgUser sysUser) {
        SysUser user = new SysUser();
        user.setSourceId(sysUser.getStUserId());
        user.setRealname(sysUser.getStName());
        user.setUsername(sysUser.getStAccount());

        user.setSalt(generateRandomString());
        String encrypt = PasswordUtil.encrypt(user.getUsername(), DEFAULT_PASSWORD, user.getSalt());
        user.setPassword(encrypt);
//        user.setAvatar()  咋不保存，后期批量保存为文件，源数据是猜测是base64
//        user.setBirthday()  没有
        user.setSex(SexEnum.value(sysUser.getStSex()).getCodeInt());
        user.setEmail(sysUser.getStEmail());
        user.setPhone(sysUser.getStMobile());
//        user.setOrgCode();//要生成
        AccountStatusEnum anEnum = AccountStatusEnum.of(sysUser.getStAccountStatus());
        if (AccountStatusEnum.DELETE.equals(anEnum)) {
            user.setStatus(AccountStatusEnum.FREEZE.getState());
            user.setDelFlag(AccountStatusEnum.DELETE.getState());
        } else {
            user.setStatus(AccountStatusEnum.NORMAL.getState());
            user.setDelFlag(AccountStatusEnum.UNKNOWN.getState());
        }

        user.setWorkNo(sysUser.getStWorkNum());
//        user.setPost();//暂时没有
        user.setTelephone(sysUser.getStPhone());
        user.setCreateBy(sysUser.getStCreateId());
        user.setCreateTime(sysUser.getDtCreateTime());
        user.setUpdateBy(sysUser.getStUpdateId());
        user.setUpdateTime(sysUser.getDtUpdateTime());
//        user.setActivitiSync();//暂时先不给默认值
        user.setUserIdentity(2);//暂且给0
        user.setDepartIds(sysUser.getStTeamId());
        user.setAccountType(AccountTypeEnum.FUNCTIONARY.getValue());
        return user;
    }

    @Override
    public Collection<SysUser> converts(Collection<org.xinjiang.data.migration.xinjiang.entity.WegovOrgUser> ss) {
        Collection<SysUser> collection = new ArrayList<>();
        if (CollectionUtils.isEmpty(ss)) {
            return collection;
        }
        for (org.xinjiang.data.migration.xinjiang.entity.WegovOrgUser s : ss) {
            collection.add(convert(s));
        }
        return collection;
    }


}
